package com.yuyu.demo.demo_springboot.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * AOP简单使用类
 * @author iv1733
 * Aspect 代表这是一个切面
 * 切面，切入点，织入
 */
@Aspect
@Component
public class ServiceAspect {
	
	private static Logger logger = LoggerFactory.getLogger(ServiceAspect.class);
	
	/**
	 * 这是一个切点 
	 * excution表示执行
	 * *表示任意返回值
	 * (..)表示任意参数
	 */
	@Pointcut("execution(public * com.yuyu.demo.demo_springboot.service.AspectService.*(..))")
	public void log(){
	}
	
	@Before("log() && args(page)")
	//表示拦截带RequestMapping注解的方法
	//@Before(value = "aspectMethod() && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void before(JoinPoint joinPoint,int page) {
		logger.info("参数=="+page);
		logger.info("前置通知=======");
	}
	
	@After("log()")
	public void after() {
		logger.info("后置通知======");
	}
	
	@AfterThrowing("log()")  
    public void doAfterThrow(){  
        logger.info("例外通知======");  
    }  
	
	@AfterReturning(returning="object",pointcut= "log()")
	public void afterReturning(Object object) {
		logger.info("最终通知======");
	}
	
	@Around(value = "log()")
	public Object arround(ProceedingJoinPoint pjp) throws Throwable{  
        logger.info("环绕通知开始======");  
        Object object = pjp.proceed();//执行该方法  
        logger.info("环绕通知完毕======"+object.toString());  
        return object;  
    } 
}
